#include "gfxUnitEmitter.h"
#include "gfxUnit.h"
#include <math.h>
#include <QPen>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>

#define UNIT_ICON_WIDTH  54
#define UNIT_ICON_HEIGHT 52
#define UNIT_ICON_HSPACING 2
#define UNIT_ICON_VSPACING 2
#define EMITTER_HEIGHT   (UNIT_ICON_HEIGHT+UNIT_ICON_VSPACING*2)


GFXUnitEmitter::GFXUnitEmitter(ClientEntity *entity, QGraphicsItem *parent)
   : GFXItem(entity, parent)
{
   mCollapsed = false;
}
/*
GFXUnitEmitter::GFXUnitEmitter(const EntityBaseStats &stats, QGraphicsItem *parent)
   : GFXItem(stats, parent)
{
}
*/


GFXUnitEmitter::~GFXUnitEmitter()
{
}

void GFXUnitEmitter::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
   int index = (event->pos().x()-UNIT_ICON_HSPACING*2)/UNIT_ICON_WIDTH; // this is slightly off
   qDebug() << "GFXUnitEmitter::mousePressEvent index=" << index;

   if (event->button() == Qt::RightButton)
   {
      if (index==0)
         mCollapsed = !mCollapsed;
   }
}

QRectF GFXUnitEmitter::boundingRect() const
{
   // If clicked on first box, toggle collapse
   // otherwise left click
   qint32 newSize = 1;
   if (mEntity)
      newSize = (mCollapsed) ? 1 : mEntity->emitterCapacity;
   return QRectF(0,0, newSize*UNIT_ICON_WIDTH+10, EMITTER_HEIGHT);
}


void GFXUnitEmitter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
   qint32 newSize = 1;
   if (mEntity)
   {
      painter->save();
      if (mCollapsed)
      {
         qreal offset = UNIT_ICON_HSPACING;
         painter->setPen(QPen(Qt::white,2));
         painter->fillRect(QRect(offset, UNIT_ICON_VSPACING, UNIT_ICON_WIDTH+UNIT_ICON_HSPACING, EMITTER_HEIGHT-UNIT_ICON_VSPACING*2), QColor(30,30,30));
         painter->drawRect(QRect(offset, UNIT_ICON_VSPACING, UNIT_ICON_WIDTH+UNIT_ICON_HSPACING, EMITTER_HEIGHT-UNIT_ICON_VSPACING*2));

         painter->translate(UNIT_ICON_WIDTH/2+UNIT_ICON_HSPACING, UNIT_ICON_HEIGHT/2);
         if (!mEntity->emitterContents.isEmpty())
         {
            EntitySquadType squad = mEntity->emitterContents.front();
            GFXUnit::doPaint(QColor(squad.stats.color), squad.stats.size, squad.stats.kind, (qint32)squad.size, *painter);
         }
      }
      else
      {
         for (unsigned int i=0; i<mEntity->emitterCapacity; ++i) // replace with capacity
         {
            qreal offset = i*(UNIT_ICON_WIDTH + UNIT_ICON_HSPACING)+UNIT_ICON_HSPACING;
            painter->setPen(Qt::green);
            if (i==0)
            {
               painter->setPen(QPen(Qt::white,2));
               painter->fillRect(QRect(offset, UNIT_ICON_VSPACING, UNIT_ICON_WIDTH+UNIT_ICON_HSPACING, EMITTER_HEIGHT-UNIT_ICON_VSPACING*2), QColor(30,30,30));
            }
            else
               painter->setPen(Qt::green);
            painter->drawRect(QRect(offset, UNIT_ICON_VSPACING, UNIT_ICON_WIDTH+UNIT_ICON_HSPACING, EMITTER_HEIGHT-UNIT_ICON_VSPACING*2));
         }

         painter->translate(UNIT_ICON_WIDTH/2+UNIT_ICON_HSPACING, UNIT_ICON_HEIGHT/2);
         foreach (EntitySquadType squad, mEntity->emitterContents)
         {
            GFXUnit::doPaint(QColor(squad.stats.color), squad.stats.size, squad.stats.kind, (qint32)squad.size, *painter);
            painter->translate(UNIT_ICON_WIDTH+UNIT_ICON_HSPACING, 0);
         }
      }
      painter->restore();
      newSize = (mCollapsed) ? 1 : mEntity->emitterCapacity;
   }


   doPaint(QColor(Qt::green),
           newSize,
           *painter,
           mPulsate ? mPulsateScale : 1.0);
}


void GFXUnitEmitter::tick()
{
   GFXItem::tick();
   if (mEntity)
   {
      setPos(mEntity->pos.toPoint());
      setRotation(mEntity->rotation);
      mStats = mEntity->base;
      //qDebug() << "GFXUnitEmitter::tick rot=" << mEntity->rotation;
   }
}


void GFXUnitEmitter::doPaint(const QColor &color, const qint32 size, QPainter& painter, const qreal scale)
{
   QPen pen(color,2);
   pen.setStyle(Qt::DotLine);
   painter.setPen(pen);
   painter.drawRect(0,0, size*(UNIT_ICON_WIDTH+UNIT_ICON_HSPACING)+UNIT_ICON_HSPACING*2, EMITTER_HEIGHT);
}
